﻿using MyCode.Project.Infrastructure.Constant;
using MyCode.Project.Infrastructure.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCode.Project.Domain.Businesses.WorkProcess
{
    /// <summary>
    /// 通知模板基类
    /// </summary>
    public abstract class NotificationTemplateBase
    {
        /// <summary>
        /// 业务说明
        /// </summary>
        public string First { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 参数字典
        /// </summary>
        protected Dictionary<string, object> ParamDict = new Dictionary<string, object>();

        /// <summary>
        /// 备注模板
        /// </summary>
        protected string Template { get; set; }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        protected void Add(string key, object value)
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }

            if (value==null || value.SafeString().IsEmpty())
            {
                return;
            }

            ParamDict.Add(key, value);
        }

        /// <summary>
        /// 初始化参数值
        /// </summary>
        protected virtual void InitParamValue() { }

        /// <summary>
        /// 格式化备注模板
        /// </summary>
        /// <returns></returns>
        public void FormatRemarkTemplate()
        {
            if (Template.IsEmpty())
            {
                return;
            }
            InitParamValue();
            var tpl = Template;
            foreach (var param in ParamDict)
            {
                var value = param.Value.SafeString();
                tpl = tpl.Replace(param.Key, value);
            }

            tpl = tpl.Replace(LxmConst.WechatTemplate.LINE_KEY, LxmConst.WechatTemplate.LINE);

            Remark = tpl;
        }

        /// <summary>
        /// 设置 备注模板
        /// </summary>
        /// <param name="template">短信模板</param>
        public void SetTemplate(string template)
        {
            Template = template;
        }
    }
}
